import { Component } from 'react';
import { onEmit, onListen } from '../../utils/request'
import { topicConstants } from '../../constants/topicConstants'


class Index extends Component {

    constructor(props) {
        super(props);
        this.state = {};
    }

    componentDidMount() {
        this.onTrigger()
    }

    onTrigger = () => {

        onListen(topicConstants.onTrigger, (response) => {
            if (!response.success) {
                return
            }
            const code = response.body.plugin.cmdInfo.featureInfo.code
            if (code == "switchWallpaper") {
                this.doSwitchWallpaper(response.body)
            }
        })
    }


    doSwitchWallpaper = (request) => {
        fetch(`https://api.unsplash.com/photos?client_id=1c0018090c0878f9556fba12d4b8ba060866de2733de1cc8486c720bf7c9a04e&order_by=latest&page=1&per_page=20`)
            .then(res => res.json())
            .then(body => {
                const item = body[Math.floor((Math.random() * body.length))]
                window.api.setWallPaperByUrl(item.urls.raw, item.id)
            })

    }


    render() {
        return (<div></div >);
    }
}


export default Index;